package cn.bluethink;


import cn.bluethink.utile.AuthenticationUtil;
import cn.bluethink.utile.GraphicalUtil;
import cn.bluethink.view.BrowseView;
import cn.bluethink.view.GraphView;
import cn.bluethink.view.LoginView;
import cn.bluethink.view.RegisteredView;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;

public class MapFrame {

	private JFrame frame = new JFrame();
	public static GraphView canvas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MapFrame window = new MapFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MapFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("BlueThink-Frame");
		frame.getContentPane().setFont(new Font("宋体", Font.PLAIN, 24));
		frame.setFont(new Font("宋体", Font.PLAIN, 24));
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setFont(new Font("宋体", Font.PLAIN, 24));
		frame.setJMenuBar(menuBar);
		
		JMenu menuFile = new JMenu("文件");
		menuBar.add(menuFile);

		JMenuItem menuOpen = new JMenuItem("读取");
		menuOpen.setIcon(new ImageIcon(MapFrame.class.getResource("/res/folder.gif")));
		menuFile.add(menuOpen);
		menuOpen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (AuthenticationUtil.isAuthentication()){
					BrowseView.showGraphical();
				}else {
					JOptionPane.showMessageDialog(null, "请你先登录", "出错啦", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JMenuItem menuSave = new JMenuItem("保存");
		menuFile.add(menuSave);
		menuSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					if (GraphicalUtil.saveGraphInfo("save",null,canvas.mGraphs.get(0)) == null){
						JOptionPane.showMessageDialog(null, "请你先登录", "出错啦", JOptionPane.ERROR_MESSAGE);
					};
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		JMenu menuGraph = new JMenu("图形");
		menuBar.add(menuGraph);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setFont(new Font("宋体", Font.PLAIN, 24));
		frame.getContentPane().add(toolBar, BorderLayout.NORTH);
		
		JButton btnOpen = new JButton("");
		toolBar.add(btnOpen);
		btnOpen.setToolTipText("打开文件");
		btnOpen.setIcon(new ImageIcon(MapFrame.class.getResource("/res/folder.gif")));

		JButton btnReset = new JButton("");
		toolBar.add(btnReset);
		btnReset.setToolTipText("全部显示");
		btnReset.setIcon(new ImageIcon(MapFrame.class.getResource("/res/mapreset.png")));

		JButton btnLogin = new JButton("登录");
		toolBar.add(btnLogin);
		btnLogin.setToolTipText("登录");
		btnLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				LoginView loginView = new LoginView();
				loginView.buildRegisteredView();
			}
		});

		JButton btnRegistered = new JButton("注册");
		toolBar.add(btnRegistered);
		btnRegistered.setToolTipText("注册");
		btnRegistered.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				RegisteredView registeredView = new RegisteredView();
				registeredView.buildRegisteredView();
			}
		});

		JButton btnZoomin = new JButton("");
		toolBar.add(btnZoomin);
		btnZoomin.setToolTipText("放大");
		btnZoomin.setIcon(new ImageIcon(MapFrame.class.getResource("/res/mapzoomin.png")));

		JButton btnZoomout = new JButton("");
		toolBar.add(btnZoomout);
		btnZoomout.setToolTipText("缩小");
		btnZoomout.setIcon(new ImageIcon(MapFrame.class.getResource("/res/mapzoomout.png")));
		
		JButton btnPan = new JButton("");
		toolBar.add(btnPan);
		btnPan.setToolTipText("漫游");
		btnPan.setIcon(new ImageIcon(MapFrame.class.getResource("/res/mappan.png")));
		
		JToolBar toolBarGraph = new JToolBar();
		toolBar.add(toolBarGraph);
		
		JButton btnGEditor = new JButton("");
		btnGEditor.setToolTipText("编辑");
		btnGEditor.setIcon(new ImageIcon(MapFrame.class.getResource("/res/editor.gif")));
		toolBarGraph.add(btnGEditor);
		btnGEditor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				canvas.mCurrentOp = "select";
			}
		});
		
		JButton btnGPoint = new JButton("");
		toolBarGraph.add(btnGPoint);
		btnGPoint.setIcon(new ImageIcon(MapFrame.class.getResource("/res/point.gif")));
		btnGPoint.setToolTipText("采集点");
		
		JButton btnGLine = new JButton("");
		toolBarGraph.add(btnGLine);
		btnGLine.setIcon(new ImageIcon(MapFrame.class.getResource("/res/line.gif")));
		btnGLine.setToolTipText("采集线段");
		btnGLine.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				canvas.mCurrentOp = "line";
			}
		});
		
		JButton btnGPolyline = new JButton("");
		toolBarGraph.add(btnGPolyline);
		btnGPolyline.setToolTipText("采集折线");
		btnGPolyline.setIcon(new ImageIcon(MapFrame.class.getResource("/res/polyline.gif")));
		btnGPolyline.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				canvas.mCurrentOp = "polyline";
			}
		});
		
		JButton btnGPolygon = new JButton("");
		toolBarGraph.add(btnGPolygon);
		btnGPolygon.setToolTipText("采集多边形");
		btnGPolygon.setIcon(new ImageIcon(MapFrame.class.getResource("/res/polygon.gif")));
		btnGPolygon.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				canvas.mCurrentOp = "polygon";
			}
		});
		
		canvas = new GraphView();
		frame.getContentPane().add(canvas, BorderLayout.CENTER);
		
		frame.addComponentListener(new ComponentAdapter(){
			@Override public void componentResized(ComponentEvent e){
				canvas.onSizeChanged();
			}});
	}

}
